home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / xgrasp.zip / XGRASP.C < prev    next >
C/C++ Source or Header  |  1992-09-04  |  6KB  |  230 lines

  1. #ident "@(#)xgrasp.c    1.8 91/04/01 XGRASP"
  2. /*-
  3.  * xgrasp.c - main routines for grasp viewer.
  4.  *
  5.  * Copyright (c) 1991 by Patrick J. Naughton
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software and its
  8.  * documentation for any purpose and without fee is hereby granted,
  9.  * provided that the above copyright notice appear in all copies and that
  10.  * both that copyright notice and this permission notice appear in
  11.  * supporting documentation.
  12.  *
  13.  * This file is provided AS IS with no warranties of any kind.  The author
  14.  * shall have no liability with respect to the infringement of copyrights,
  15.  * trade secrets or any patents by this file or any part thereof.  In no
  16.  * event will the author be liable for any lost revenue or profits or
  17.  * other special, indirect and consequential damages.
  18.  *
  19.  * Comments and additions should be sent to the author:
  20.  *
  21.  *                     Patrick J. Naughton
  22.  *                     Sun Microsystems
  23.  *                     2550 Garcia Ave, MS 10-20
  24.  *                     Mountain View, CA 94043
  25.  *                     (415) 336-1080
  26.  *
  27.  */
  28.  
  29. #include "grasp.h"
  30. #include <X11/Xatom.h>
  31.  
  32. char       *pname;
  33. int         imageloop = 0;
  34. int         showdirectory = 0;
  35. int         showtext = 0;
  36. int         printthecodes = 0;
  37. int         verbose = 0;
  38. int         imverbose = 0;
  39. Display    *dsp;
  40. Window      win;
  41. Visual     *vis;
  42. XVisualInfo vinfo;
  43. int         screen;
  44. int         planes;
  45. GC          gc;
  46. GC          gc1;
  47. long        white;
  48. long        black;
  49. Atom        protocol_atom;
  50. Atom        kill_atom;
  51.  
  52.  
  53. void
  54. error(s1, s2)
  55.     char       *s1, *s2;
  56. {
  57.     fprintf(stderr, s1, pname, s2);
  58.     exit(1);
  59. }
  60.  
  61. void
  62. outi(s, i)
  63.     char       *s;
  64.     int         i;
  65. {
  66.     fprintf(stderr, "%s: %d\n", s, i);
  67. }
  68.  
  69. void
  70. outs(s1, s2)
  71.     char       *s1, *s2;
  72. {
  73.     fprintf(stderr, "%s: %s\n", s1, s2);
  74. }
  75.  
  76.  
  77. u_int
  78. GetByte(fp)
  79.     FILE       *fp;
  80. {
  81.     return (u_int) getc(fp);
  82. }
  83.  
  84. u_int
  85. GetWord(fp)
  86.     FILE       *fp;
  87. {
  88.     u_char      b1 = (u_char) getc(fp);
  89.     u_char      b2 = (u_char) getc(fp);
  90.  
  91.     return (u_int) (b1 + b2 * 256);
  92. }
  93.  
  94. u_int
  95. GetLong(fp)
  96.     FILE       *fp;
  97. {
  98.     u_char      b1 = (u_char) getc(fp);
  99.     u_char      b2 = (u_char) getc(fp);
  100.     u_char      b3 = (u_char) getc(fp);
  101.     u_char      b4 = (u_char) getc(fp);
  102.     return (u_int) (b1 + b2 * 256 + b3 * 256 * 256 + b4 * 256 * 256 * 256);
  103. }
  104.  
  105.  
  106. FilenameStruct *
  107. readdirectory(fp, count)
  108.     FILE       *fp;
  109.     int        *count;
  110. {
  111.     FilenameStruct *fn;
  112.     int         i;
  113.     int         len = GetWord(fp);
  114.  
  115.     *count = (len / 17) - 1;
  116.     fn = (FilenameStruct *) malloc(*count * sizeof(FilenameStruct));
  117.     for (i = 0; i < *count; i++) {
  118.     fn[i].offset = GetLong(fp);
  119.     fread(fn[i].fname, 13, 1, fp);
  120.     fn[i].fname[13] = 0;
  121.     lowerstr(fn[i].fname);
  122.     if (showdirectory)
  123.         fprintf(stderr, "%13s   %6d\n", fn[i].fname, fn[i].offset);
  124.     }
  125.     return (fn);
  126. }
  127.  
  128. usage()
  129. {
  130.     error("usage: %s [-display dsp] [-verbose] graspfile\n", NULL);
  131. }
  132.  
  133. main(argc, argv)
  134.     int         argc;
  135.     char       *argv[];
  136. {
  137.     char       *displayName = 0;
  138.     XSetWindowAttributes xswa;
  139.     XFontStruct *xfont;
  140.     XGCValues   gcv;
  141.     XWMHints    xwmh;
  142.     Pixmap      pix;
  143.     char       *filename = 0;
  144.     FILE       *fp;
  145.     FilenameStruct *dir;
  146.     int         count;
  147.     int         i;
  148.  
  149.     pname = argv[0];
  150.     for (i = 1; i < argc; i++) {
  151.     char       *s = argv[i];
  152.     int         n = strlen(s);
  153.  
  154.     if (!strncmp("-display", s, n))
  155.         displayName = argv[++i];
  156.     else if (!strncmp("-dir", s, n))
  157.         showdirectory = 1;
  158.     else if (!strncmp("-textout", s, n))
  159.         showtext = 1;
  160.     else if (!strncmp("-images", s, n))
  161.         imageloop = 1;
  162.     else if (!strncmp("-verbose", s, n))
  163.         verbose = 1;
  164.     else if (!strncmp("-imverbose", s, n))
  165.         imverbose = 1;
  166.     else if (!strncmp("-printcodes", s, n))
  167.         printthecodes = 1;
  168.     else if (filename || s[0] == '-')
  169.         usage();
  170.     else
  171.         filename = s;
  172.     }
  173.     if (!filename)
  174.     usage();
  175.  
  176.     if (!(dsp = XOpenDisplay(displayName)))
  177.     error("%s: unable to open display \"%s\".\n",
  178.           XDisplayName(displayName));
  179.  
  180.     protocol_atom = XInternAtom(dsp, "WM_PROTOCOLS", False);
  181.     kill_atom = XInternAtom(dsp, "WM_DELETE_WINDOW", False);
  182.  
  183.     screen = DefaultScreen(dsp);
  184.     gc = DefaultGC(dsp, screen);
  185.     white = WhitePixel(dsp, screen);
  186.     black = BlackPixel(dsp, screen);
  187.     planes = DisplayPlanes(dsp, screen);
  188.     xfont = XLoadQueryFont(dsp, "8x13");
  189.     XSetFont(dsp, gc, xfont->fid);
  190.     pix = XCreatePixmap(dsp, RootWindow(dsp, screen), 1, 1, 1);
  191.     gcv.foreground = black;
  192.     gcv.background = white;
  193.     if (!(gc1 = XCreateGC(dsp, pix, GCForeground | GCBackground, &gcv)))
  194.     error("%s: couldn't create mono gc.\n");
  195.     XFreePixmap(dsp, pix);
  196.  
  197.     if (!XMatchVisualInfo(dsp, screen, planes, PseudoColor, &vinfo))
  198.     if (!XMatchVisualInfo(dsp, screen, planes, GrayScale, &vinfo))
  199.         if (!XMatchVisualInfo(dsp, screen, planes, DirectColor, &vinfo))
  200.         error("%s: only works on color displays\n");
  201.     vis = vinfo.visual;
  202.  
  203.     xswa.event_mask = ExposureMask | KeyPressMask |
  204.     ButtonPress | StructureNotifyMask;
  205.     xswa.backing_store = Always;
  206.     win = XCreateWindow(dsp,
  207.             RootWindow(dsp, screen),
  208.             0, 0, 1, 1, 0,
  209.             DefaultDepth(dsp, screen),
  210.             InputOutput,
  211.             vis,
  212.             CWEventMask | CWBackingStore,
  213.             &xswa);
  214.     XStoreName(dsp, win, filename);
  215.     XSetWMProtocols(dsp, win, &kill_atom, 1);
  216.     xwmh.flags = InputHint;
  217.     xwmh.input = True;
  218.     XChangeProperty(dsp, win, XA_WM_HINTS, XA_WM_HINTS, 32,
  219.            PropModeReplace, (u_char *) &xwmh, sizeof(xwmh) / sizeof(int));
  220.  
  221.     fp = fopen(filename, "rb");
  222.     if (!fp)
  223.     error("%s: %s not found.\n", filename);
  224.     dir = readdirectory(fp, &count);
  225.     readfiles(fp, dir, count);
  226.     fclose(fp);
  227.     execfile(exec[0], 0);
  228.     exit(0);
  229. }
  230.